/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.gui.blocks.parametrized;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author tiella
 */
public class ParametrizedBlockImpl extends ParametrizedBlock {

    private List<String> attrNames = new ArrayList<>();
    private Map<String, String> bindings = new HashMap<>();
    private Map<String, String> formats = new HashMap<>();
    private Map<String, String> units = new HashMap<>();
    private Map<String, Boolean> editable = new HashMap<>();
    private Map<String, Boolean> showInLabels = new HashMap<>();
    private Map<String, String> labels = new HashMap<>();
    private Map<String, TypeSpec> types = new HashMap<>();
    private Map<String, String> codeTemplates = new HashMap<>();

    

    public String getCodeTemplate(String pLang) {
        return codeTemplates.get(pLang);
    }

    public void setCodeTemplate(String pLang, String pseudoCode) {
        this.codeTemplates.put(pLang, pseudoCode);
    }

    public void addAttribute(String name, TypeSpec ts, String label, String defaultValue,
            String u, boolean editable, boolean showInLabel) {

        attrNames.add(name);
        formats.put(name, label + ": %s\n");
        bindings.put(name, defaultValue);
        units.put(name, u);
        this.editable.put(name, editable);
        labels.put(name, label);
        types.put(name, ts);
        showInLabels.put(name, showInLabel);


    }

    public List<String> getAttributeNames() {
        return attrNames;
    }

    public String getAttributeValueAsString(String n) {
        return bindings.get(n);
    }

    public String getUnits(String n) {
        return units.get(n);
    }

    @Override
    public String getText() {

        StringBuilder sb = new StringBuilder();

        if (attrNames != null && attrNames.size() > 0) {

            // sb.append("<html>");
            
            for (String key : attrNames) {

                if (showInLabels.get(key)) {

                    String text = String.format(formats.get(key), 
                            types.get(key).getStringForValue(bindings.get(key)));

                    sb.append(text);
                    sb.append(" ");
                    sb.append(units.get(key));
                    // sb.append("<br/>");
                    sb.append("@");

                }
                
               

            }
            
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length()-1);
            }

        }

        return sb.toString();

    }

    @Override
    public void serializeAttributes(StringBuilder buffer) {
        
        super.serializeAttributes(buffer);

        for (String n : attrNames) {


            buffer.append(String.format("attr.%s=%s\n", n, bindings.get(n)));

        }

    }

    @Override
    public void setAttribute(String attrName, String attrValue) {

        if (attrName.startsWith("attr.")) {

            String[] s = attrName.split("\\.");

            bindings.put(s[1], attrValue);
        }

    }

    public boolean isAttributeEditable(String n) {
        return editable.get(n);
    }

    public String getAttributeLabel(String n) {
        return labels.get(n);
    }

    public String get(String attrName) {

        return getAttributeValueAsString(attrName);

    }

    public void setAttributeValueFromString(String an, String attributeValue) {

        bindings.put(an, attributeValue);

    }

    public TypeSpec getAttributeType(String attrName) {
        return types.get(attrName);

    }
    
    public void dump(PrintWriter pw) {

        super.dump(pw);

        for (String n : attrNames) {
            pw.println("attribute: " + n + " " + bindings.get(n));
        }


    }
    
}
